﻿<template>
    <el-dialog :close-on-click-modal="false" :close-on-press-escape="false" title="编辑" :append-to-body="true" :visible="modalEdit" @close="modalEdit=false" @opened="opened" @closed="closed">
        <el-form ref="customer" label-position="right" label-width="120px" :model="customer" :rules="rules">
            <input type="hidden" :model="customer.id" />
            <!-- 
            <el-form-item label="业务员" prop="userId">
                <el-select size="small" v-model="customer.userId" style="width: 240px" :loading="searchLoading" placeholder="输入姓名等操作" filterable>
                    <el-option v-for="user in users" :key="user.id" :label="user.realName" :value="user.id">
                        {{user.realName}}
                    </el-option>
                </el-select>
                <el-button size="small" @click="remoteMethod" :loading="searchLoading">获取</el-button>
            </el-form-item>
            <el-form-item label="业务助理" prop="userAgentId">
                <el-select size="small" v-model="customer.userAgentId" style="width: 240px" :loading="searchLoading2" placeholder="输入姓名等操作" filterable>
                    <el-option v-for="user in users2" :key="user.id" :label="user.realName" :value="user.id">
                        {{user.realName}}
                    </el-option>
                </el-select>
                <el-button size="small" @click="remoteMethod2" :loading="searchLoading2">获取</el-button>
            </el-form-item> -->
            <el-form-item label="客户编码" prop="customerCode">
                <el-input size="small" v-model="customer.customerCode" :readonly="customer.id>0" placeholder="客户编码" style="width: 240px" />
            </el-form-item>
            <el-form-item label="客户名称" prop="name">
                <el-input size="small" v-model="customer.name" placeholder="客户名称" style="width: 240px" />
            </el-form-item>
            <el-form-item label="客户简称" prop="shortName">
                <el-input size="small" v-model="customer.shortName" placeholder="客户简称" style="width: 240px" />
            </el-form-item>
            <el-form-item label="联系人" prop="linkMan">
                <el-input size="small" v-model="customer.linkMan" placeholder="联系人" style="width: 240px" />
            </el-form-item>
            <el-form-item label="联系电话" prop="phone">
                <el-input size="small" v-model="customer.phone" placeholder="联系电话" style="width: 240px" />
            </el-form-item>
            <el-form-item label="测试单号" prop="deliveryNo">
                <el-input size="small" v-model="customer.deliveryNo" placeholder="测试单号" style="width: 240px" />
            </el-form-item>
            <el-form-item label="备注" prop="remark">
                <el-input size="small" v-model="customer.remark" placeholder="备注" style="width: 240px" />
            </el-form-item>
            <el-form-item label="生产日期字段" prop="createTimeField">
                <el-input size="small" v-model="customer.createTimeField" placeholder="生产日期字段" style="width: 240px" />
            </el-form-item>
            <el-form-item label="批次字段" prop="batchField">
                <el-input size="small" v-model="customer.batchField" placeholder="批次字段" style="width: 240px" />
            </el-form-item>
            <el-form-item label="转时间戳字段" prop="timeField">
                <el-input size="small" v-model="customer.timeField" placeholder="转时间戳字段" style="width: 240px" />
            </el-form-item>
            <el-form-item label="状态" prop="state">
                <el-radio-group v-model="customer.state">
                    <el-radio :label="1">启用</el-radio>
                    <el-radio :label="0">禁用</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item label="验证客户单号" prop="pn">
                <el-radio-group v-model="customer.pn">
                    <el-radio :label="1">验证</el-radio>
                    <el-radio :label="0">忽略</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item label="验证批次" prop="dc">
                <el-radio-group v-model="customer.dc">
                    <el-radio :label="1">验证</el-radio>
                    <el-radio :label="0">忽略</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item label="验证生产日期" prop="sc">
                <el-radio-group v-model="customer.sc">
                    <el-radio :label="1">验证</el-radio>
                    <el-radio :label="0">忽略</el-radio>
                </el-radio-group>
            </el-form-item>
        </el-form>
        <div slot="footer">
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" :loading="modalLoading" @click="ok">确定</el-button>
        </div>
    </el-dialog>
    </div>
</template>
<script>
export default {
    name: 'Customer',
    data() {
        return {
            searchLoading: false,
            searchLoading2: false,
            modalEdit: false,
            modalLoading: false,
            customerId: "",
            customer: {
                userId: null,
                state: 1,
                userAgentId: null,
                type: 1
            },
            users: [],
            users2: [],
            rules: {
                shortName: [{ required: true, message: "简称必填" }],
                name: [{ required: true, message: "客户名称必填" }],
                userId: [{ required: true, message: "业务必填" }],
                userAgentId: [{ required: true, message: "业务助理必填" }],
                customerId: [{ required: true, message: "业务员必选" }]
            }
        };
    },
    mounted: function() {


    },
    methods: {
        remoteMethod: function() {

            this.searchLoading = true;
            this.$http.post("/sys/sysuser/search.action", { roleId: null }).then(response => {
                this.searchLoading = false;
                var body = response;
                if (body.status == 0) {
                    this.users = body.obj;
                } else {
                    this.errorMessage(body.info);
                }
            }, response => {
                this.errorMessage("异常！")
            });

        },
        remoteMethod2: function() {

            this.searchLoading2 = true;
            this.$http.post("/sys/sysuser/search.action", { roleId: null }).then(response => {
                this.searchLoading2 = false;
                var body = response;
                if (body.status == 0) {
                    this.users2 = body.obj;
                } else {
                    this.errorMessage(body.info);
                }
            }, response => {
                this.errorMessage("异常！")
            });

        },
        opened() {

            this.getDetail();
            if (this.users.length <= 0) {
                this.remoteMethod();
            }
            if (this.users2.length <= 0) {
                this.remoteMethod2();
            }
        },
        closed: function() {
            this.customer = {
                userId: null,
                state: 1,
                type: 1
            };
            this.searchLoading = false;
        },
        showDialog(id) {
            this.customerId = id;
            this.modalEdit = true;
        },
        getDetail: function() {
            if (this.customerId == null || this.customerId == undefined || this.customerId == "") return;
            var me = this;
            me.showLoading();
            me.$http.post("/sys/customer/show_" + me.customerId + ".action").then(response => {
                var body = response;
                if (body.status == 0) {
                    me.customer = body.obj;
                }
                me.hideLoading();
            }, response => {
                me.hideLoading();
            });
        },
        ok: function() {
            var ok = false;
            this.$refs['customer'].validate((valid) => {
                ok = valid;
                if (valid) {

                } else {
                    this.errorMessage('请认真填写数据');
                }
            })
            if (!ok) return;
            var me = this;
            me.modalLoading = true;
            me.customer.type = 1;
            me.$http.post("/sys/customer/doSave.action", me.customer).then(response => {
                var body = response;
                if (body.status == 0) {
                    me.successMessage("保存成功！");
                    me.$emit("ok");
                    me.modalEdit = false;


                } else {
                    me.errorMessage(body.info);
                }
                me.modalLoading = false;
            }, response => {
                me.modalLoading = false;
            });
        },
        cancel: function() {
            this.customer = {};
            this.modalEdit = false;
            this.$emit("cancel");
        }
    }
}
</script>
<style scoped>
</style>